package com.sskj.login.ui.activity;

import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.github.yoojia.inputs.LazyLoaders;
import com.hey.lib.HeySpinner;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.enums.AreaCodeEnum;
import com.sskj.lib.box.LiveDataBus;
import com.sskj.lib.box.verifier.ToastNextInputs;
import com.sskj.lib.util.CaptchaUtil;
import com.sskj.lib.util.EditUtil;
import com.sskj.lib.util.SchemeUtil;
import com.sskj.lib.util.SendCodeUtil;
import com.sskj.login.R;
import com.sskj.login.R2;
import com.sskj.login.presenter.RegisterActivityPresenter;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

/**
 * 注册
 */
@Route(path = RConfig.LOGIN_REGISTER)
public class RegisterActivity extends BaseActivity<RegisterActivityPresenter> {

    @BindView(R2.id.etAccount)
    EditText etAccount;
    @BindView(R2.id.etCheckCode)
    EditText etCheckCode;
    @BindView(R2.id.tvCheckCode)
    TextView tvCheckCode;
    @BindView(R2.id.etPwd)
    EditText etPwd;
    @BindView(R2.id.ivPwdToggle)
    ImageView ivPwdToggle;
    @BindView(R2.id.etPwdAgain)
    EditText etPwdAgain;
    @BindView(R2.id.ivPwdToggleAgain)
    ImageView ivPwdToggleAgain;
    @BindView(R2.id.etInviteCode)
    EditText etInviteCode;
    @BindView(R2.id.checkbox)
    CheckBox checkbox;
    @BindView(R2.id.tvAgreement)
    TextView tvAgreement;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    private Disposable timeDis;
    private String phoneNumber, messageCode, loginPwd, visitCode;
    ToastNextInputs inputs;
    private ToastNextInputs inputsCode;
    private AreaCodeEnum areaCodeEnumChoose;
    @BindView(R2.id.heySpinner)
    HeySpinner heySpinner;

    @Override
    public int getLayoutId() {
        return R.layout.login_activity_register;
    }

    @Override
    public RegisterActivityPresenter getPresenter() {
        return new RegisterActivityPresenter();
    }

    @Override
    public boolean getImmersion() {
        return false;
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        inputs = new ToastNextInputs();
        inputsCode = new ToastNextInputs();
        inputs.clear();
        inputsCode.clear();
        inputsCode.add(etAccount, SchemeUtil.notEmpty(etAccount), SchemeUtil.phone())
        ;
        inputs.add(etAccount, SchemeUtil.notEmpty(etAccount), SchemeUtil.phone())

                .add(etCheckCode, SchemeUtil.notEmpty(etCheckCode), SchemeUtil.smsCode())
                .add(etPwd, SchemeUtil.notEmpty(etPwd), SchemeUtil.pwd())
                .add(etPwdAgain, SchemeUtil.notEmpty(etPwdAgain), SchemeUtil.equalsPwd(LazyLoaders.fromEditText(etPwd)))
                .add(etInviteCode, SchemeUtil.notEmpty(etInviteCode))
                .add(checkbox, SchemeUtil.checkbox(checkbox))
        ;
        heySpinner.setVisibility(Constans.isAreaCode ? View.VISIBLE : View.GONE);

        SendCodeUtil.areaCode(this, heySpinner, areaCodeEnum -> areaCodeEnumChoose = areaCodeEnum);

    }

    @Override
    protected void initEvent() {
        ClickUtil.click(tvCheckCode, () -> {
            if (inputsCode.test()) {
                registerCheck();
            }
        });
        ClickUtil.click(200, tvAgreement, () -> {
            ARouter.getInstance().build(RConfig.HANG_GUIDE_WEB)
                    .withBoolean(Constans.IS_REGISTER_AGREEMENT, true)
                    .navigation();
        });

        ClickUtil.click(btSubmit, () -> {
            if (inputs.test()) {
                phoneNumber = getText(etAccount);

                messageCode = getText(etCheckCode);
                loginPwd = getText(etPwd);
                visitCode = getText(etInviteCode);
                mPresenter.register(phoneNumber, loginPwd, visitCode, messageCode, areaCodeEnumChoose.getAreaCode());
            }
        });

        EditUtil.togglePs(etPwd, ivPwdToggle);
        EditUtil.togglePs(etPwdAgain, ivPwdToggleAgain);
    }

    public void sendCheckCodeSuccess() {
        ToastUtil.showShort("操作成功");
        tvCheckCode.setEnabled(false);
        DisposUtil.close(timeDis);
        timeDis = SendCodeUtil.timeCount(this, tvCheckCode);
    }


    public void registerSuccess() {
        ToastUtil.showShort("注册成功");
        LiveDataBus.get().with(RxBusCode.REGISTER_ACCOUNT, String.class)
                .postValue(etAccount.getText().toString());
        finish();
    }

    public void registerCheck() {
        CaptchaUtil.check(this, (result, validate, msg) -> {
            mPresenter.sendCode(validate, etAccount.getText().toString(), areaCodeEnumChoose.getAreaCode());
        });
    }
}
